package com.shwread.android.qysw.activity.psychological;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.GlideUtil;
import com.NewActionHelp;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.newui.activity.EvaluateWebActivity;
import com.shwread.android.qysw.bean.psychological.EvaluatingDetailBean;
import com.shwread.android.qysw10000229.R;
import com.shwread.android.ui.widget.refresh.SuperSwipeRefreshLayout;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.List;

/**
 * 评测列表
 * Created by sss on 2017/10/9.
 */
public class EvaluatingListFragment extends Fragment {
    private static final String MODULE_ID = "moduleId";

    private List<EvaluatingDetailBean> infoList;
    private RecyclerView rlvEvaluatingList;
    private EvaluatingAdapter evaluatingAdapter;
    private Activity mActivity;
    private int moduleId;

    /**
     * 动画页面
     */
    private RelativeLayout commonLoadingLayout;
    /**
     * 加载动画
     */
    private ImageView loadingImg;
    /**
     * 无数据
     */
    private TextView tvNotData;

    private SuperSwipeRefreshLayout layoutSwipeRefresh;
    // Header View
    private ProgressBar progressBar;
    private TextView textView;
    private ImageView imageView;

    public static EvaluatingListFragment newInstance(int moduleId) {
        EvaluatingListFragment fragment = new EvaluatingListFragment();
        Bundle args = new Bundle();
        args.putInt(MODULE_ID, moduleId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_evaluating_list_view, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rlvEvaluatingList = (RecyclerView) view.findViewById(R.id.rlv_evaluating_list);

        loadingImg = (ImageView) view.findViewById(R.id.common_loading__img);
        tvNotData = (TextView) view.findViewById(R.id.tv_not_data);
        commonLoadingLayout = (RelativeLayout) view.findViewById(R.id.common_loading_layout);
        layoutSwipeRefresh = (SuperSwipeRefreshLayout) view.findViewById(R.id.layout_swipe_refresh);
        layoutSwipeRefresh.setHeaderView(createHeaderView());
        layoutSwipeRefresh.setOnPullRefreshListener(new SuperSwipeRefreshLayout.OnPullRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {
                textView.setText("正在刷新");
                imageView.setVisibility(View.GONE);
                progressBar.setVisibility(View.VISIBLE);
                getListData();
            }

            @Override
            public void onPullDistance(int distance) {
            }

            @Override
            public void onPullEnable(boolean enable) {
                textView.setText(enable ? "松开刷新" : "下拉刷新");
                imageView.setVisibility(View.VISIBLE);
                imageView.setRotation(enable ? 180 : 0);
            }
        });
        evaluatingAdapter = new EvaluatingAdapter();
        rlvEvaluatingList.setAdapter(evaluatingAdapter);
        if (getArguments() == null) {
            return;
        }
        moduleId = getArguments().getInt(MODULE_ID);
        getListData();
    }

    private View createHeaderView() {//设置头布局
        View headerView = LayoutInflater.from(layoutSwipeRefresh.getContext()).inflate(R.layout.layout_head, null);
        progressBar = (ProgressBar) headerView.findViewById(R.id.pb_view);
        textView = (TextView) headerView.findViewById(R.id.text_view);
        textView.setText("下拉刷新");
        imageView = (ImageView) headerView.findViewById(R.id.image_view);
        imageView.setVisibility(View.VISIBLE);
        imageView.setImageResource(R.drawable.xlistview_arrow);
        progressBar.setVisibility(View.GONE);
        return headerView;
    }

    private void getListData() {
        NewActionHelp.getMoreQuestionnaireByModuleId(mActivity, moduleId, new GetMoreQuestionnaireByModuleIdHandler());
    }

    private class GetMoreQuestionnaireByModuleIdHandler extends ObjectHttpResponseHandler<List<EvaluatingDetailBean>> {
        @Override
        public void onFailure(int i, Header[] headers, String responseString, Throwable throwable) {
            Log.e("sss", "失败---" + responseString);
            progressBar.setVisibility(View.GONE);
            layoutSwipeRefresh.setRefreshing(false);

            commonLoadingLayout.setVisibility(View.VISIBLE);
            loadingImg.setBackgroundResource(R.drawable.home_loading_fail);
        }

        @Override
        public void onSuccess(int i, Header[] headers, List<EvaluatingDetailBean> infos, String responseString) {
            progressBar.setVisibility(View.GONE);
            layoutSwipeRefresh.setRefreshing(false);
            Log.e("sss", "成功---" + responseString);
            if (infos == null || infos.size() == 0) {
                commonLoadingLayout.setVisibility(View.VISIBLE);
                loadingImg.setBackgroundResource(R.drawable.bg_region_blank);
                tvNotData.setText("没有数据");
                tvNotData.setVisibility(View.VISIBLE);
                return;
            }
            commonLoadingLayout.setVisibility(View.GONE);
            infoList = infos;
            evaluatingAdapter.notifyDataSetChanged();
        }

        @Override
        public Type getType() {
            return new TypeToken<List<EvaluatingDetailBean>>() {
            }.getType();
        }
    }


    private class EvaluatingAdapter extends RecyclerView.Adapter<EvaluatingHolder> {
        @Override
        public EvaluatingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View customView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_evaluating_list, parent, false);
            return new EvaluatingHolder(customView);
        }

        @Override
        public void onBindViewHolder(EvaluatingHolder holder, final int position) {
            final EvaluatingDetailBean info = infoList.get(position);

            holder.tvEvaluatingPrice.setVisibility(View.VISIBLE);
            holder.btEvaluatingPay.setVisibility(View.VISIBLE);

            GlideUtil.filletedCornerLoad(mActivity, info.getImgUrl(), 8, holder.imgEvaluatingCover);
            holder.tvEvaluatingType.setVisibility(info.getType() == 1 ? View.GONE : View.VISIBLE);
            if ("n".equals(info.getIsBuy())) {//未购买
                holder.btEvaluatingPay.setText("购买");
                holder.btEvaluatingPay.setBackgroundResource(R.drawable.evaluating_buy_btn);
                holder.tvEvaluatingPrice.setTextColor(0xfff71d74);
                holder.tvEvaluatingPrice.setText(String.format("¥%s", info.getPrice()));
            } else if ("y".equals(info.getIsBuy())) {//已购买
                holder.btEvaluatingPay.setText("测评");
                holder.btEvaluatingPay.setBackgroundResource(R.drawable.action_btn);
                holder.tvEvaluatingPrice.setTextColor(0xffb1b1b1);
                holder.tvEvaluatingPrice.setText("已购");
            }
            holder.tvEvaluatingTitle.setText(info.getTitle());
            holder.btEvaluatingPay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转网页
                    if ("n".equals(info.getIsBuy())) {
                        EvaluatingPayUtils.getInstance().pay(mActivity, info, new EvaluatingPayUtils.Listener() {
                            @Override
                            public void CallBack() {
                                getListData();
                            }
                        });
                    } else {
                        Intent intent = new Intent(mActivity, EvaluateWebActivity.class);
                        intent.putExtra("title", "心理测评");
                        startActivity(intent);
                    }
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {//点击事件
                    Intent intent = new Intent(mActivity, EvaluatingDetailActivity.class);
                    intent.putExtra("qryType", EvaluatingDetailActivity.PAY_EVALUATING);//固定为付费评测
                    intent.putExtra("questionnaireId", info.getQuestionnaireId());
                    mActivity.startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return infoList == null ? 0 : infoList.size();
        }
    }

    private static class EvaluatingHolder extends RecyclerView.ViewHolder {

        private ImageView imgEvaluatingCover;
        private TextView tvEvaluatingType;
        private TextView tvEvaluatingTitle;
        private TextView tvEvaluatingPrice;
        private Button btEvaluatingPay;

        private EvaluatingHolder(View itemView) {
            super(itemView);
            imgEvaluatingCover = (ImageView) itemView.findViewById(R.id.img_evaluating_cover);
            tvEvaluatingType = (TextView) itemView.findViewById(R.id.tv_evaluating_type);
            tvEvaluatingTitle = (TextView) itemView.findViewById(R.id.tv_evaluating_title);
            tvEvaluatingPrice = (TextView) itemView.findViewById(R.id.tv_evaluating_price);
            btEvaluatingPay = (Button) itemView.findViewById(R.id.bt_evaluating_pay);
        }
    }
}
